package com.jxnu.nav_search.adapters;

import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;


import android.content.Context;
import android.view.LayoutInflater;
import android.widget.TextView;


import com.jxnu.nav_search.R;
import com.jxnu.nav_search.utils.Datasets;

import java.util.ArrayList;
import java.util.List;


public class RecyclerBaseAdapter extends RecyclerView.Adapter<RecyclerBaseAdapter.ViewHolder> {

    private int layout;

    private Context context;

    //the data
    private List<Datasets> mData;

    private OnItemClickListener mOnItemClickListener = null;

    private ViewDataBinding viewDataBinding;


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }

    //define interface
    public interface OnItemClickListener {
        void onItemClick(int position, int layout);
    }


    public RecyclerBaseAdapter(List<Datasets> data, int layout) {
        this.mData = data;
        this.layout = layout;
    }

    public void updateData(ArrayList<Datasets> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    /**
     * 初始化设置
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        // find view
        viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(context), layout, parent, false);

        // set view
        ViewHolder viewHolder = new ViewHolder(viewDataBinding.getRoot());
        //return holder
        return viewHolder;
    }

    /**
     * 数据的绑定
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        // bind data
        if (layout == R.layout.fragment_list) {
            holder.title.setText(mData.get(position).toString());
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    /**
     * 视图holder
     */
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView title;


        public ViewHolder(View itemView) {
            super(itemView);

            if (layout == R.layout.fragment_list) {
                title = itemView.findViewById(R.id.tv_4);
            }
        }
    }
}
